package com.pi.pro.xcmg.am.common;



import com.pi.common.CodesConstants;
import com.pi.common.utils.StringUtils;
import com.pi.pro.xcmg.am.common.vo.CommonExchangeResultData;
import com.pi.pro.xcmg.am.common.vo.act.MoveChangeDto;
import com.pi.pro.xcmg.am.common.vo.act.PalletizeChangeData;
import com.pi.pro.xcmg.am.common.vo.edi.XwmsInventoryLineDto;
import com.pi.pro.xcmg.am.common.vo.report.CommonDocumentDetailsDto;
import com.pi.pro.xcmg.am.common.vo.report.CommonInventoryDetailsDto;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.dto.base.LocationLineDto;
import com.pi.wms.edi.common.ExchangeConstants;
import com.pi.wms.entity.TwtActTaskList;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


/**
 * 常量定义
 */
public class XcmgAmSupport {



    public final static CommonExchangeResultData<T> ok(){
        return ok(null, "");
    }

    public final static CommonExchangeResultData<T> ok(T data, String key){
        CommonExchangeResultData<T> resultData = new CommonExchangeResultData<T>();
        resultData.setReturestate(ExchangeConstants.REPLY_MESSAGE_STATE_SUCCESS);
        resultData.setReturecode(CodesConstants.COMMON_STATUS_SUCCESS);
        resultData.setReturetext("成功");
        resultData.setKey(key);
        resultData.setData(data);
        return resultData;
    }

    public final static CommonExchangeResultData error(String errorCode, String errorText, String key){
        CommonExchangeResultData<T> resultData = new CommonExchangeResultData<T>();
        if (StringUtils.isNullOrBlank(errorCode)){
            errorCode=CodesConstants.COMMON_STATUS_APP_ERROR;
        }
        if (StringUtils.isNullOrBlank(errorText)){
            errorText="发生异常.";
        }
        resultData.setReturestate(ExchangeConstants.REPLY_MESSAGE_STATE_ERROR);
        resultData.setReturecode(errorCode);
        resultData.setReturetext(errorText);
        resultData.setKey(key);
        resultData.setData(null);
        return resultData;
    }

    public final static CommonExchangeResultData error(String errorText){
        return error(CodesConstants.COMMON_STATUS_APP_ERROR, errorText, "");
    }



    /******************************************************************************************************/
    /**
     * STATION
     * /
     ******************************************************************************************************/

    //PAL-RECV-REQ
    public final static Map<String, String> palletizeRequestStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101PalletizeReqStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101PalletizeReqStations;
        }
        return XcmgAmConstants.wh101PalletizeReqStations;
    }

    //SHIP-CLOSE
    public final static Map<String, String> shipCloseStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101ShipCloseStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101ShipCloseStations;
        }
        return XcmgAmConstants.wh101ShipCloseStations;
    }

    //MOVEIN-REQ
    public final static Map<String, String> moveinRequestStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101ShipCloseStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101ShipCloseStations;
        }
        return XcmgAmConstants.wh101ShipCloseStations;
    }

    //EPAL-MOVEIN-REQ
    public final static Map<String, String> epalMoveinRequestStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101EpalReqStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101EpalReqStations;
        }
        return XcmgAmConstants.wh101EpalReqStations;
    }

    //SRM-DROP/
    public final static Map<String, String> srmDropStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101SrmDropStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101SrmDropStations;
        }
        return XcmgAmConstants.wh101SrmDropStations;
    }

    //EPAL-REPL
    public final static Map<String, String> epalReplenishStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101EpalReplStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101EpalReplStations;
        }
        return XcmgAmConstants.wh101EpalReplStations;
    }

    //MOVE-CLOSE
    public final static Map<String, String> moveCloseStation(String divisionCode){
        if (StringUtils.isNullOrBlank(divisionCode)){
            return XcmgAmConstants.wh101MoveCloseStations;
        }
        if (XcmgAmConstants.DIVISION_WH101_CODE.equalsIgnoreCase(divisionCode.trim())){
            return XcmgAmConstants.wh101MoveCloseStations;
        }
        return XcmgAmConstants.wh101MoveCloseStations;
    }

    public static String workSourceNameOf(String code) {
        String name = "外购物料";
        if (StringUtils.isNullOrBlank(code)){
            return name;
        }
        if (XcmgAmConstants.WORK_SOURCE_PO.equalsIgnoreCase(code)){
            name = "外购物料";
            return name;
        }
        if (XcmgAmConstants.WORK_SOURCE_KIT.equalsIgnoreCase(code)){
            name = "生产配盘";
            return name;
        }
        if (XcmgAmConstants.WORK_SOURCE_SELF_MADE.equalsIgnoreCase(code)){
            name = "自制物料";
            return name;
        }
        if (XcmgAmConstants.WORK_SOURCE_OTHER.equalsIgnoreCase(code)){
            name = "其他物料";
            return name;
        }

        return name;
    }


    public static String sameAsNull(String text){
        if (StringUtils.isNullOrBlank(text)){
            return null;
        }
        return text;
    }

    public static String check(MoveChangeDto changeDto){
        String m = "";
        if (changeDto==null){
            m="请求数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(changeDto.getLocationCode())){
            m="请求数据无效,站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(changeDto.getContainerNumber())){
            m="请求数据无效,托盘码为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(changeDto.getChangeType())){
            m="请求数据无效,请求类型为空.";
            return m;
        }
        return m;
    }

    public static String check(XwmsInventoryLineDto palletizeData){
        String m = "";
        if (palletizeData==null){
            m="码盘数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(palletizeData.getMaterialcode())){
            m="码盘数据无效,物料码为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(palletizeData.getContainernumber())){
            m="码盘数据无效,托盘码为空.";
            return m;
        }
        if ((palletizeData.getWorkqty()==null) || (palletizeData.getWorkqty().compareTo(BigDecimal.ZERO)<=0)){
            m="码盘数据无效,无效的码盘数量,码盘数量必须大于0.";
            return m;
        }
        return m;
    }

    public static String check(PalletizeChangeData palletizeData){
        String m = "";
        if (palletizeData==null){
            m="码盘数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(palletizeData.getMaterialcode())){
            m="码盘数据无效,物料码为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(palletizeData.getContainnumber())){
            m="码盘数据无效,托盘码为空.";
            return m;
        }
        if ((palletizeData.getQuantity()==null) || (palletizeData.getQuantity().compareTo(BigDecimal.ZERO)<=0)){
            m="码盘数据无效,无效的码盘数量,码盘数量必须大于0.";
            return m;
        }
        return m;
    }

    public static TwtActTaskList createReturnTask(String taskNumber, MoveChangeDto moveChangeDto, TaskActionDto actionDto,
                                                  LocationLineDto fromLocation, LocationLineDto toLocation, String errorText){
        TwtActTaskList task=null;

        if (moveChangeDto==null){
            return task;
        }
        if (fromLocation==null){
            return task;
        }
        if (toLocation==null){
            return task;
        }
        if (StringUtils.isNullOrBlank(taskNumber)){
            taskNumber=moveChangeDto.getRequestNumber();
        }
        try{
            task=new TwtActTaskList();
            task.setNumber(taskNumber);
            if (actionDto!=null){
                task.setGlobalClientId(actionDto.getClientID());
                task.setDivisionId(actionDto.getDivisionId());
                task.setCustomerId(actionDto.getCustomerId());
                if (!StringUtils.isNullOrBlank(actionDto.getTaskNumber())){
                    task.setNumber(actionDto.getTaskNumber());
                }
            }
            if (task.getGlobalClientId()==null){
                task.setGlobalClientId(fromLocation.getGlobalClientId());
            }
            if (task.getCustomerId()==null){
                task.setCustomerId(XcmgAmConstants.CUSTOMER_ID_XCMG_AM);
            }
            if (task.getDivisionId()==null){
                task.setDivisionId(fromLocation.getDivisionId());
            }

            task.setNumberSeq("0010");
            task.setCategory(WmsConstants.TASK_TYPE_MOVE);
            task.setType(WmsConstants.TASK_TYPE_MOVE);

            task.setTaskMode(XcmgAmConstants.TASK_MODE_RETURN_ACTION);
            task.setTaskClass(null);
            //task.setTaskAction(XcmgAmConstants.WCS_TASK_ACTION_RETURN);
            task.setTaskFeature(null);
            task.setGroupNumber(null);
            task.setMaterialId(XcmgAmConstants.MATERIAL_ID_EPAL);
            task.setDocId(null);
            task.setDocDetailsId(null);
            task.setWaveId(null);
            task.setWaveDetailsId(null);
            task.setDocNumber(null);
            task.setDocLinenumber(null);
            task.setStatus(WmsConstants.TASK_STATUS_CREATE);
            task.setStorageId(null);
            task.setFromLotnoId(null);
            task.setFromLotnoNumber(null);
            //
            task.setFromLocId(fromLocation.getLocationId());
            task.setFromLocSeq(fromLocation.getLocationCode());
            task.setFromContainNumber(moveChangeDto.getContainerNumber());
            task.setFromTraceNumber(moveChangeDto.getTraceNumber());

            task.setFromQty(BigDecimal.ONE);
            task.setFromQtyEach(task.getFromQty());
            task.setFromPackId(null);
            task.setFromUomCode(null);
            task.setFromPrice(null);
            task.setFromGrossweight(null);
            task.setFromNetweight(null);
            task.setFromCubic(null);

            task.setToDivisionId(task.getDivisionId());
            task.setToLotnoId(task.getFromLotnoId());
            task.setToLotnoNumber(task.getFromLotnoNumber());
            //
            task.setToLocId(toLocation.getLocationId());
            task.setToLocSeq(toLocation.getLocationCode());
            task.setToContainNumber(task.getFromContainNumber());
            task.setToTraceNumber(task.getFromTraceNumber());

            task.setToQty(task.getFromQty());
            task.setToQtyEach(task.getToQty());
            task.setToPackId(task.getFromPackId());
            task.setToUomCode(task.getFromUomCode());
            task.setToPrice(task.getFromPrice());
            task.setToGrossweight(task.getFromGrossweight());
            task.setToNetweight(task.getFromNetweight());
            task.setToCubic(task.getFromCubic());

            //
            task.setPlantoLocId(task.getToLocId());
            task.setPlantoContainNumber(null);
            task.setPlantoTraceNumber(null);
            task.setPlantoQty(null);
            task.setPlantoQtyEach(task.getPlantoQty());
            task.setPlantoPackId(null);
            task.setPlantoUomCode(null);
            task.setPlantoLocSeq(task.getToLocSeq());
            task.setTaskPrior(WmsConstants.TASK_PRIOR_NORMAL);
            task.setTaskSchema(null);
            task.setReleaseStatus(CodesConstants.COMMON_RELEASE_STATUS_CREATED);
            task.setPrintStatus(null);
            task.setBatchNumber(actionDto.getBatchNumber());
            task.setBulkNumber(null);
            task.setRefType(null);
            task.setRefId(null);
            task.setRefAllocationId(null);
            task.setRefParentTaskId(null);
            task.setRefPreTaskId(null);
            task.setRefNextTaskId(null);
            task.setRefOpenTransId(null);
            task.setRefCloseTransId(null);
            task.setOpenTime(null);
            task.setOpenBy(null);
            task.setPlantoTime(null);
            task.setPlantoBy(null);
            task.setTakeTime(null);
            task.setTakeBy(null);
            task.setHoldTime(null);
            task.setHoldBy(null);
            task.setCloseTime(null);
            task.setCloseBy(null);
            task.setErrorCode(CodesConstants.COMMON_STATUS_APP_ERROR);
            task.setErrorMessages(errorText);
            task.setLotattr01(null);
            task.setLotattr02(null);
            task.setLotattr03(null);
            task.setLotattr04(null);
            task.setLotattr05(null);
            task.setLotattr06(null);
            task.setLotattr07(null);
            task.setLotattr08(null);
            task.setLotattr09(null);
            task.setLotattr10(null);
            task.setLotattr11(null);
            task.setLotattr12(null);
            task.setLotattr13(null);
            task.setLotattr14(null);
            task.setLotattr15(null);
            task.setLotattr16(null);

            task.setUdf01(null);
            task.setUdf02(null);
            task.setUdf03(null);
            task.setUdf04(null);
            task.setUdf05(null);
            task.setUdf06(null);
            task.setUdf07(null);
            task.setUdf08(null);
            task.setUdf09(null);
            task.setUdf10(null);

            task.setEdiSendTime(null);
            task.setEdiSendStatus(CodesConstants.COMMON_STATUS_CREATED);
            task.setEdiReceiveTime(null);
            task.setEdiReceiveStatus(null);
            task.setEdiReceiveMsg(null);

            task.setDelFlag(false);
            task.setEditable(1);
            task.setNotes("异常回退/"+errorText);

            task.setCreatedDate(new Date());
            task.setCreatedBy(CodesConstants.SYSTEM_USER_RCS);
            if ((actionDto!=null) && (!StringUtils.isNullOrBlank(actionDto.getWorker()))){
                task.setCreatedBy(actionDto.getWorker());
            }
            task.setLastModifiedDate(task.getCreatedDate());
            task.setLastModifiedBy(task.getCreatedBy());
        }catch (Exception e){
            e.printStackTrace();
        }
        return task;
    }


    public final static void asnStatusOf(CommonDocumentDetailsDto line){
        if (line==null){
            return;
        }
        if (StringUtils.isNullOrBlank(line.getDocumentstatus())){
            line.setDocumentstatus(WmsConstants.ASN_STATUS_CREATE);
        }
        if (StringUtils.isNullOrBlank(line.getLinestatus())){
            line.setLinestatus(WmsConstants.ASN_STATUS_CREATE);
        }
        String name=XcmgAmConstants.asnStatus.get(line.getDocumentstatus());
        if (StringUtils.isNullOrBlank(name)){
            name="创建";
        }
        line.setDocumentstatusname(name);
        //
        name=XcmgAmConstants.asnStatus.get(line.getLinestatus());
        if (StringUtils.isNullOrBlank(name)){
            name="创建";
        }
        line.setLinestatusname(name);
    }

    public final static void soStatusOf(CommonDocumentDetailsDto line){
        if (line==null){
            return;
        }
        if (StringUtils.isNullOrBlank(line.getDocumentstatus())){
            line.setDocumentstatus(WmsConstants.SO_STATUS_CREATE);
        }
        if (StringUtils.isNullOrBlank(line.getLinestatus())){
            line.setLinestatus(WmsConstants.SO_STATUS_CREATE);
        }
        String name=XcmgAmConstants.soStatus.get(line.getDocumentstatus());
        if (StringUtils.isNullOrBlank(name)){
            name="创建";
        }
        line.setDocumentstatusname(name);
        //
        name=XcmgAmConstants.soStatus.get(line.getLinestatus());
        if (StringUtils.isNullOrBlank(name)){
            name="创建";
        }
        line.setLinestatusname(name);
    }

    public final static void inventoryStatusOf(CommonInventoryDetailsDto line){
        if (line==null){
            return;
        }
        if (StringUtils.isNullOrBlank(line.getStoragestatus())){
            line.setStoragestatus(WmsConstants.STORAGE_STATUS_AVAILABLE);
        }
        String name=XcmgAmConstants.storageStatus.get(line.getStoragestatus());
        if (StringUtils.isNullOrBlank(name)){
            name="可用";
        }
        line.setStoragestatusname(name);
    }

}
